package eppic.pkg;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.content.ContentValues;
import android.content.Context;

public class Client {
	
	public int id;
	public String company;
	public String client;
	public float rate;
	public String email;
	public String phone;
	public String notes;
	
	public Client(int id, String company, String client, float rate, String email, String phone, String notes)
	{
		this.id = id;
		this.company = company;
		this.client = client;
		this.rate = rate;
		this.email = email;
		this.phone = phone;
		this.notes = notes;
	
	}
	
	public Client(String company, String client, float rate, String email, String phone, String notes)
	{
		this.company = company;
		this.client = client;
		this.rate = rate;
		this.email = email;
		this.phone = phone;
		this.notes = notes;
	
	}
	
	public Client(int id, Context context)
	{
		DatabaseHelper dbHelper = new DatabaseHelper(context);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		
        Cursor c=db.rawQuery("SELECT * FROM clients WHERE _id="+id, null);
        c.moveToFirst();
       
        this.id = id;
    	this.company = c.getString(c.getColumnIndex("company_name"));
    	this.client = c.getString(c.getColumnIndex("client_name"));
    	this.rate = c.getFloat(c.getColumnIndex("rate"));
    	this.email = c.getString(c.getColumnIndex("email"));
    	this.phone = c.getString(c.getColumnIndex("phone"));
    	this.notes = c.getString(c.getColumnIndex("notes"));


        c.close();
        db.close();
	}
	
	public Client()
	{
		this.company = "";
		this.client = "";
		this.email = "";
		this.phone = "";
		this.notes = "";
	}
	
	public Client getClientById(int id, Context context)
	{
		DatabaseHelper dbHelper = new DatabaseHelper(context);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		
        Cursor c=db.rawQuery("SELECT * FROM clients WHERE _id="+id, null);
        c.moveToFirst();
       
    	String company = c.getString(c.getColumnIndex("company_name"));
    	String client_name = c.getString(c.getColumnIndex("client_name"));
    	String email = c.getString(c.getColumnIndex("email"));
    	float rate = c.getFloat(c.getColumnIndex("rate"));
    	String phone = c.getString(c.getColumnIndex("phone"));
    	String notes = c.getString(c.getColumnIndex("notes"));
	        	
	        	
	    Client client = new Client(id, company, client_name, rate, email, phone, notes);

        c.close();
        db.close();

		return client;
	}
	
	public boolean delete(Context context)
	{
		DatabaseHelper dbHelper = new DatabaseHelper(context);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        
        try{
        	db.execSQL("DELETE FROM clients WHERE _id="+this.id);
        }catch(Exception e)
        {
        	return false;
        }
        
        try{
        	db.execSQL("DELETE FROM projects WHERE client_id="+this.id);
        }catch(Exception e)
        {
        	return false;
        }
        
        try{
        	db.execSQL("DELETE FROM logs WHERE parent_id="+this.id+" AND is_project = 0");
        }catch(Exception e)
        {
        	return false;
        }
        
        try{
        	db.execSQL("DELETE FROM expenses WHERE parent_id="+this.id+" AND is_project = 0");
        }catch(Exception e)
        {
        	return false;
        }
        
        
		return true;
	}
	
	public void setCompany(String company)
	{
		this.company = company;
	}
	
	public boolean save(Context context)
	{
		DatabaseHelper dbHelper = new DatabaseHelper(context);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues cv=new ContentValues();
        
        //Values
		cv.put("company_name", this.company);
		cv.put("client_name", this.client);
		cv.put("rate", this.rate);
		cv.put("email", this.email);
		cv.put("phone", this.phone);
		cv.put("notes", this.notes);
		
		if(db.insert("clients", "notes", cv) != -1)
		{
			db.close();
			return true;
		}
		else
		{
			db.close();
			return false;
		}
	
	}
	
	public boolean update(Context context)
	{
		DatabaseHelper dbHelper = new DatabaseHelper(context);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues cv=new ContentValues();
        
        //Values
		cv.put("company_name", this.company);
		cv.put("client_name", this.client);
		cv.put("rate", this.rate);
		cv.put("email", this.email);
		cv.put("phone", this.phone);
		cv.put("notes", this.notes);
		
		if(db.update("clients", cv, "_id="+this.id, null) != -1)
		{
			db.close();
			return true;
		}
		else
		{
			db.close();
			return false;
		}
	}
	
	public Log[] getLogs(Context context)
	{
		DatabaseHelper dbHelper = new DatabaseHelper(context);
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor c=db.rawQuery("SELECT * FROM logs WHERE parent_id = "+this.id+" AND is_project = 0", null);
       
        int count = c.getCount();
        Log[] all = new Log[count];
        
        c.moveToFirst();
        if(count > 0)
        {
        	for(int i = 0; i < count; i++)
        	{
	        	int id = c.getInt(c.getColumnIndex("_id"));
	        	int parent_id = c.getInt(c.getColumnIndex("parent_id"));
	        	int is_project = c.getInt(c.getColumnIndex("is_project"));
	        	long start = c.getLong(c.getColumnIndex("start"));
	        	long end = c.getLong(c.getColumnIndex("end")); 	
	        	
	        	Log temp_log = new Log(id, parent_id, is_project, start, end);
	            all[i] = temp_log;
	            c.moveToNext();
        	}
        }
        
        c.close();
        db.close();

		return all;
		
	}
	
	
	public Expense[] getExpenses(Context context)
	{
		DatabaseHelper dbHelper = new DatabaseHelper(context);
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor c=db.rawQuery("SELECT * FROM expenses WHERE parent_id = "+this.id+" AND is_project = 0", null);
       
        int count = c.getCount();
        Expense[] all = new Expense[count];
        
        c.moveToFirst();
        if(count > 0)
        {
        	for(int i = 0; i < count; i++)
        	{
	        	int id = c.getInt(c.getColumnIndex("_id"));
	        	int parent_id = c.getInt(c.getColumnIndex("parent_id"));
	        	int is_project = c.getInt(c.getColumnIndex("is_project"));
	        	String name = c.getString(c.getColumnIndex("name"));
	        	float cost = c.getFloat(c.getColumnIndex("cost")); 	
	        	
	        	Expense temp_expense = new Expense(id, parent_id, is_project, name, cost);
	            all[i] = temp_expense;
	            c.moveToNext();
        	}
        }
        
        c.close();
        db.close();

		return all;
		
	}
	
}
	
	

